# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

from odoo import api, fields, models, _


class MrpProduction(models.Model):
    _inherit = 'mrp.production'

    change_ids = fields.Many2many('sb.order.internal.change', string='Order Internal Change')
    change_count = fields.Integer(compute='_compute_change_count', store=True, string='Order Internal Change Count')

    @api.depends('change_ids')
    def _compute_change_count(self):
        for rec in self:
            rec.change_count = len(rec.change_ids)

    def action_internal_changes(self):
        action = {
            'name': _('Order Internal Changes'),
            'view_mode': 'tree,form',
            'view_type': 'form',
            'res_model': 'sb.order.internal.change',
            'target': 'current',
            'type': 'ir.actions.act_window'
        }
        if self.change_count == 1:
            action.update({
                'view_mode': 'form',
                'res_id': self.change_ids.id
            })
        else:
            action.update({
                'domain': [('id', 'in', self.change_ids.ids)]
            })
        return action


